package tgc.edu.ljc.layui.security;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import tgc.edu.ljc.layui.service.SysUserService;



@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
	
	@Autowired
	private SysUserService sysUserService;
	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
		auth
		.userDetailsService(sysUserService)
		.passwordEncoder(encoder);
		auth.inMemoryAuthentication().withUser("root").password("{noop}root").roles("DVP","ADMIN");  
	}

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.csrf().disable();
		http.formLogin()
			.loginPage("/login")
			.defaultSuccessUrl("/main")
			.failureForwardUrl("/login").permitAll();
		http.authorizeRequests().antMatchers("/static/**").permitAll();
		http.authorizeRequests().antMatchers("/webjars/**").permitAll();
		http.logout().logoutUrl("/logout").logoutSuccessUrl("/login").permitAll();
        http.authorizeRequests().anyRequest().authenticated();
	}

}
